package src.cs2340.todoApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * controls what the text fields and buttons on the login page do
 * @author Rochelle
 */
public class LoginScreen extends Activity {
	
	//the text fields that contain the username, and password that the user entered
	EditText usrName;
	EditText pswd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);		//sets the display to the login.xml layout
		
		//Instantiates text fields using respective ids
		usrName = (EditText) findViewById(R.id.usernamefield);
		pswd = (EditText) findViewById(R.id.passwordfield);
		
		//Instantiates buttons using respective ids
		Button login = (Button) findViewById(R.id.login_button);
		Button cancel = (Button) findViewById(R.id.cancelLoginButton);
		
		/**
		 * creates a Listener for cancel which sends the user back to the the initial Welcome page
		 */
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginScreen.this);
				alertDialog.setMessage("Are you sure you want to leave the login screen?");
				
				/**
				 * The code called for the "YES" option
				 */
				alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						/**will return user to welcome screen*/
						Intent intent = new Intent(getApplicationContext(), WelcomeScreen.class);
						finish();
						startActivity(intent);
					}
				});
				
				/**
				 * The code called for the "NO" option
				 */
				alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				})
				
				.show();
			}
		});
		
		/**
		 * creates a Listener for login that will check and verify that the password matches, if
		 * it is a match then the user will get logged in and brought to their account
		 */
		login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Account acc = new Account(usrName.getText().toString(), pswd.getText().toString(), null, null, Database.getInstance());
				
				if(acc.login()){
					Intent intent = new Intent(getApplicationContext(),UserTaskListScreen.class);
					// add user information parameters
					intent.putExtra("username", usrName.getText().toString());
					intent.putExtra("password", pswd.getText().toString());
					finish();
					startActivity(intent);
				}
				else{
					AlertDialog.Builder builder = new AlertDialog.Builder(LoginScreen.this);
					builder.setMessage("Username/Password Incorrect, try again")
					       .setCancelable(false)
					       .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
					           @Override
							public void onClick(DialogInterface dialog, int id) {
					        	   dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
				}
			}
		});
	}
}
